#!/bin/bash
##!/bin/bash -x

ARGS=`getopt -o hvpt:o:a:c:: \
--long help,version,package,build-shared:,build-static:,build-type:,build-os:,build-arch:,build-dir:,prefix:,qt-path:,build-qt-static:,clang:: -- "$@"`
# echo $ARGS
eval set -- "${ARGS}"

if [ $? != 0 ]; then
    echo "Terminating..."
    exit 1
fi

usage()
{
    cat <<EOF
Usage: $0 [options]
Options: [defaults in brackets after descriptions]
    -h, --help        display this help and exit
    --build-shared    enable or disable building shared [ON/OFF, ON]
    --build-static    enable or disable building static [ON/OFF, OFF]
    --build-type      set build type [debug/release/all, release]
    --build-os=OS     set build OS platform [linux/android,linux]
    --build-dir=DIR   set build directory [../build-\${build-os}]
    --prefix=PREFIX   install architecture-independent files in PREFIX [\${build_dir}/install]
    --qt-path=DIR     set CMAKE_PREFIX_PATH for qmake []
    --build-qt-static enable or disable building qt libraris static [ON/OFF, OFF]
    -p, --package     enable or disable compressed packaging, default not package
EOF
exit 1
}

assert_error()
{
    errno=$?
    if [ $errno != 0 ]; then
        echo "Building $1 with error $errno, exit..."
        exit $errno
    fi
}

source_dir=`pwd`/..

package=OFF

build_shared=ON
build_static=OFF
build_qt_static=OFF
build_type=ALL
#build_type=debug
build_os=linux
build_arch=x`getconf LONG_BIT`
prefix_dir=$source_dir/build/install

if [ ${build_static,,} = on ]; then
    build_qt_static=ON
fi
if [ ${build_qt_static,,} = on ]; then
    qt_path=/opt/Qt5.9.3.static.set2
else
    qt_path=/opt/Qt5.9.3.shared.set2
fi

while true
do
    case "$1" in
        -h|--help)
            usage
            shift
            ;;

        -v|--version)
            echo "Option version";
            shift
            ;;

        -p|--package)
            package=ON
            shift
            ;;

        --build-shared)
            build_shared=$2
            shift 2
            ;;

        --build-static)
            build_static=$2
            shift 2
            ;;

        -t|--build-type)
            build_type=${2,,}
            shift 2
            ;;

        -o|--build-os)
            build_os=$2
            shift 2
            ;;

        -a|--build-arch)
            build_arch=$2
            shift 2
            ;;

        --qt-path)
            qt_path=$2
            shift 2
            ;;

        --build-qt-static)
            build_qt_static=$2
            shift 2
            ;;

        --prefix)
            prefix_dir=$2
            shift 2
            ;;
        --build-dir)
            build_dir=$2
            shift 2
            ;;
        -c|--clong)
            case "$2" in
                "")
                    echo "Option c, no argument";
                    shift 2
                    ;;
                *)
                    echo "Option c, argument $2";
                    shift 2;
                    ;;
            esac
            ;;
        --)
            shift
            break
            ;;
        *)
            echo "unkonw option {$1}!"
            exit 1
            ;;
    esac
done

for arg in $@
do
    echo "processing $arg"
done

start=`date +%s`

if [ ${build_arch} = x32 ]; then
    packet_part2=i386
else
    packet_part2=x86_64
fi

count=1
debug_postfix=
if [ ${build_type,,} = all ]; then
    count=2
elif [ ${build_type,,} = debug ]; then
    tmp_type=debug
    debug_postfix=d
elif [ ${build_type,,} = release ]; then
    tmp_type=release
fi

for ((i=1; i<=${count}; i++))
do
    if [ ${build_type,,} = all ]; then
        if [ $i = 1 ]; then
            tmp_type=release
            debug_postfix=
        elif [ $i = 2 ]; then
            tmp_type=debug
            debug_postfix=d
        fi
    fi

    build_dir=$source_dir/build/$build_os/$build_arch/${tmp_type,,}
    mkdir -p ${build_dir}; cd ${build_dir}
    rm -fr *

    $qt_path/bin/qmake $source_dir "CONFIG+=${tmp_type,,}" "CYG_PREFIX+=$prefix_dir"
    assert_error "qmake ${tmp_type,,}"

    make -j4;
    assert_error "make -j4 ${tmp_type,,}"

    make install
    assert_error "make install ${tmp_type,,}"

    VERSION_GIT=`cat ${source_dir}/version.h |grep 'VERSION_GIT ' | awk '{print $3}'`
    if [ ${VERSION_GIT} = 1 ]; then
        VERSION_TAG=`cat ${source_dir}/version.h | grep 'VERSION_GIT_TAG '  | awk '{print $3}' | sed 's/\"//g'`
        VERSION_REV=`cat ${source_dir}/version.h | grep 'VERSION_GIT_SHA1 ' | awk '{print $3}' | sed 's/\"//g'`
        VERSION_BUILD_PTS=`cat ${source_dir}/version.h | grep 'VERSION_BUILD_PTS' | awk '{print $3}' | sed 's/\"//g'`
    else
        VERSION_TAG=`cat ${source_dir}/version.h | grep 'VERSION_SVN_TAG ' | awk '{print $3}' | sed 's/\"//g'`
        VERSION_REV=`cat ${source_dir}/version.h | grep 'VERSION_SVN_REV ' | awk '{print $3}' | sed 's/\"//g'`
        VERSION_BUILD_PTS=`cat ${source_dir}/version.h | grep 'VERSION_BUILD_PTS' | awk '{print $3}' | sed 's/\"//g'`
    fi

    if [ ${package,,} = on ]; then
        cat ${source_dir}/scripts/package/linux/package${debug_postfix}.sh | sed "s?VERSION?${VERSION_TAG}-${VERSION_REV}?g" | sed "s?STIME?${start}?g" > ${build_dir}/package${debug_postfix}-sed.sh

        cd ${prefix_dir}/linux/${build_arch}
        packet_part1=p3000${debug_postfix}-bin-${VERSION_TAG}-${VERSION_REV}-build${VERSION_BUILD_PTS}-linux
        packet_prefix=${prefix_dir}/../../pub/packet/${packet_part1}-${packet_part2}
        tar zcvf ${packet_prefix}.tar.gz p3000${debug_postfix}
        cat ${build_dir}/package${debug_postfix}-sed.sh ${packet_prefix}.tar.gz > ${packet_prefix}.run
        chmod +x ${packet_prefix}.run
        rm -f ${packet_prefix}.tar.gz
    fi
done

stop=`date +%s`
echo "Build successfull!!!"
echo "Takes $[ stop - start ] seconds!!!"
